# Maintainer: Peter Budai <peterbud@hotmail.com>

_realname=darktable
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.0.1
pkgrel=1
pkgdesc="darktable is an open source photography workflow application and raw developer (mingw-w64)"
arch=('any')
url='https://www.darktable.org'
license=('GPL3')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "po4a")
depends=("${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-flickcurl"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         $([[ "$CARCH" == "x86_64" ]] && echo "${MINGW_PACKAGE_PREFIX}-gmic")
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lensfun"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libgphoto2"
         "${MINGW_PACKAGE_PREFIX}-libsecret"
         "${MINGW_PACKAGE_PREFIX}-libsoup"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-osm-gps-map"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib"
        )
options=('!strip')
source=("https://github.com/darktable-org/${_realname}/releases/download/release-${pkgver}/${_realname}-${pkgver}.tar.xz"{,.asc})
sha256sums=('c54b3921da14a97c99ab2f79feca468cf2abca65adf907dba6216e47edab7cb7'
            'SKIP')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi
  
  LDFLAGS+=" -fopenmp"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G'MSYS Makefiles' \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBINARY_PACKAGE_BUILD=ON \
    -DBUILD_MSYS2_INSTALL=ON \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}

  make VERBOSE=1
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

  # Remove dependencies which are by default added for WIN32 builds
  # but not needed under MSYS2/MINGW due to the defined dependencies
  cd "${pkgdir}${MINGW_PREFIX}"
  find bin -maxdepth 1 -type f -not -wholename 'bin/*darktable*' -prune -exec rm -rf {} +
  find lib -mindepth 1 -type d -not -wholename 'lib/darktable*' -prune -exec rm -rf {} +
  find share/locale -mindepth 1 -type f -not -wholename 'share/locale/*/LC_MESSAGES/darktable*' -prune -exec rm -rf {} +
  rm -rf share/glib-2.0
  rm -rf share/lensfun
  rm -rf share/icons
  rm -rf share/iso-codes
}
